package com.aaa.javaweb.servlet;

import com.aaa.javaweb.dao.*;
import com.aaa.javaweb.entity.TuiFang;
import com.aaa.javaweb.entity.User;
import com.aaa.javaweb.entity.XuFang;
import com.alibaba.fastjson.support.hsf.HSFJSONUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Controller
public class FDContorller {
    @Autowired
    private TChukuDao chukuDao;
    @Autowired
    private FDDao fdDao;
    @Autowired
    private SheShiDao sheShiDao;
    @Autowired
    private OrderDao orderDao;

    @RequestMapping("allroom")
    @ResponseBody
    public List<Map> allroom() {
        List<Map> allroom = fdDao.allroom();
        return allroom;
    }

    @RequestMapping("tuifang")
    @ResponseBody
    public List<Map> tuifang(Integer rzxq) {
        /**
         * 获取当前订单你退房时剩余的房间天数
         */
        Map hashMap = new HashMap();
        List arrayList = new ArrayList();
        List<Map> tuifang = fdDao.tuifang(rzxq);
        Map map = tuifang.get(0);
        Date rzdate = (Date) map.get("rzdate");
        Date leavedate = (Date) map.get("leavedate");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String rzdateFormat = simpleDateFormat.format(rzdate);
        String leavedateFormat = simpleDateFormat.format(leavedate);
        System.out.println(rzdateFormat + "============" + leavedateFormat);
        DateTimeFormatter yyyyMMdd = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate parse = LocalDate.parse(leavedateFormat, yyyyMMdd);
        LocalDate now = LocalDate.now();
        LocalDateTime now1 = LocalDateTime.now();

        System.out.println("获取当前时间" + now1);

        Period between = Period.between(now, parse);
        //获取到的是剩余房间还剩下多少天
        int days = (between.getDays() - 1);
        System.out.println(days);
        /**
         * 获取我们要反退给他多少钱
         */
        //获取这个房间类型的价格
        Double i = fdDao.typeroomprice(rzxq);
        //获取的是状态为入住时的价格
        Double price = (Double) map.get("price");
        System.out.println(i + "=======" + price);
        //应该反退的金额
        Double fantui = days * i;
        System.out.println("反退金额为:" + fantui);
        //实际金额为
        Double implmoney = price - fantui;
        System.out.println("实际金额为:" + implmoney);

        hashMap.put("roomcode", map.get("roomid"));
        hashMap.put("orderid", map.get("orderid"));
        hashMap.put("orderstatus", map.get("status"));
        hashMap.put("rzyj", map.get("rzyj"));
        hashMap.put("syts", days);
        hashMap.put("fantui", fantui);
        hashMap.put("implmoney", implmoney);
        arrayList.add(hashMap);
        return arrayList;
    }


    @RequestMapping("lijitui")
    @ResponseBody
    public int lijitui(TuiFang tuiFang) {

        System.out.println(tuiFang.getRuzhuid());
        if (tuiFang.getSyts() > 0) {
            //修改提前退房的状态
            int i = fdDao.setstuid(tuiFang.getOrderid());
        } else {
            //修改正常退房订单的状态
            int j = fdDao.setstuids(tuiFang.getOrderid());
        }
        //再退住详情里添加一条退住信息
        int addtuizhu = fdDao.addtuizhu(tuiFang);
        //修改退房后的房间状态  为 打扫中
        int jj = fdDao.fangjianstu(tuiFang.getRoomcode());
        //int updruzhu = fdDao.updruzhustatus(tuiFang.getRuzhuid());

        return addtuizhu;

    }

    @RequestMapping("dasaofang")
    @ResponseBody
    public int dasaofang(Integer roomcode, HttpSession session) {
        //打扫房间
        User user = (User)session.getAttribute("nowStaff");
        int xHcount =sheShiDao.selectXHcount();
        int rukuid =chukuDao.addTFChuku(user.getId(),xHcount);
        if (rukuid > 0) {
            // 添加入库详情表
            int result = chukuDao.addTFChukuDetail(rukuid);
            if (result > 0) {
                // 增加商品的数量,返回影响的总行数
                int tFSSCount = chukuDao.tfchukuAndAddBookCount();
            }
        }
        int i = fdDao.dasao(roomcode);
        return i;
    }


    /**
     * 换房
     */
    @RequestMapping("huanfang")
    @ResponseBody
    public List<Map> huanfang(Integer ruzhuid) {
        //System.out.println(ruzhuid);
        //查询同种房间的
        List<Map> maps = fdDao.selectsy(ruzhuid);
        int typeid = Integer.parseInt(maps.get(0).get("typeid") + "");
        List<Map> maps1 = fdDao.selectzs(typeid);
        return maps1;

    }

    /**
     * 确定换房
     *
     * @param rzid,rmcode
     * @return
     */
    @RequestMapping("qdhf")
    @ResponseBody
    public int qdhuanfang(Integer rzid, Integer rmcode) {
        int j = 0;
        int huangfangjilu = 0;
        List<Map> maps = fdDao.selectqdhf(rzid);
        int roomid = Integer.parseInt(maps.get(0).get("roomid") + "");
        System.out.println(roomid);
        int i = fdDao.updroom(roomid, rmcode);
        int i1 = fdDao.updroom1(roomid, rmcode);
        if (i + i1 > 1) {
            j = fdDao.updxq(rzid, rmcode);
        }
        if (j > 0) {
            huangfangjilu = fdDao.huangfangjilu(roomid, rmcode, rzid);
        }

        return huangfangjilu;
    }


    @RequestMapping("xufang")
    @ResponseBody
    public Date xufang(Integer ruzhuid) {
        Date getEndtime = fdDao.getEndtime(ruzhuid);
        return getEndtime;
    }

    /**
     * 确定续房，续房时要判断获取到的续房时间段有没有满房时间，如果有那就没有办法续房
     *
     * @param xuFang
     */

    @RequestMapping("quedingxufang")
    @ResponseBody
    public int quedingxufang(XuFang xuFang) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //获取到续房开始的时间
        String begin = simpleDateFormat.format(xuFang.getBegin());
        //获取到续房结束的时间
        String endtime = simpleDateFormat.format(xuFang.getEndtime());
        System.out.println(begin+"======="+endtime);
        //根据ruzhuid 获取到当前的房间类型
        List<Map> selroomtype = fdDao.selroomtype(xuFang.getRuzhuid());

        int i = Integer.parseInt(selroomtype.get(0).get("typeid") + "");
        System.out.println("房间类型:"+i);
        /**
         * 续房时有条件，一个入住详情只能续一次房,不能多次续房
         */
        //查找它是否有续房记录如果没有就可以续房，如果有就不能续房
        List<Map> xufangjilu = fdDao.selxufangjilu(xuFang.getRuzhuid());
        System.out.println("啊啊啊啊啊啊"+xufangjilu);
        if (xufangjilu.size() > 0) {
            //如果他有续房记录那么就不能让他续房
            return 0;
        } else {
            /**
             * 这样的话我们就需要获取到它的续房时间判断一下它再续房的所有时间段里是否有满房的时间，如果有那么也不能续房
             */
            //获取两个时间的所有时间
            orderDao.allNowandMaxtime(begin, endtime);
            //查询出来所有的时间
            List<Map> selalltime = orderDao.selAlltime();
            //查询这些时间里面满房的时间
            Map getDateOrder = orderDao.getDateOrder(selalltime,i);
            System.out.println("______________________"+getDateOrder.get("maxDate"));
            List maxDate = (List) getDateOrder.get("maxDate");
            System.out.println("%%%%%%%%%%%%%%"+maxDate);


            if (maxDate.size() > 0) {
                return 0;
            } else {
                /**
                 * 可以续房,查出要交多少钱.
                 */

                //查询续房的天数
                SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyyMMdd");
                String begin1 = simpleDateFormat1.format(xuFang.getBegin());
                String endtime1 = simpleDateFormat1.format(xuFang.getEndtime());

                System.out.println(begin1+"$$$$$$$$$$$$$$$$$"+endtime1);
                DateTimeFormatter yyyyMMdd = DateTimeFormatter.ofPattern("yyyyMMdd");
                LocalDate beginDate = LocalDate.parse(begin1, yyyyMMdd);
                LocalDate endtimedate = LocalDate.parse(endtime1, yyyyMMdd);
                Period between = Period.between(beginDate, endtimedate);
                int days = between.getDays();
                //查询当前房间的房间价格
                Double jiage = fdDao.typeroomprice(xuFang.getRuzhuid());
                Double xufangjiage = days * jiage;

                int xufangyes = fdDao.insertxufang(xuFang.getRuzhuid(),begin,endtime,xufangjiage);
                return xufangyes;
            }


        }

    }


}

















